package model;

import java.util.HashMap;
import java.util.Map;

/**
 * Class uses Factory Pattern in order to create employee types.
 * Class instance gets employee's details and permission, and returns a new
 * employee or manager object according to the permission
 */
public class EmployeeFactory {

	private interface Factory {
		
		/**
		 * Creates a new employee with given details. 
		 * Object returned will be an employee or a manager, according to the permission
		 * @param number- new employee's number
		 * @param name- new employee's name
		 * @param userName- new employee's user-name
		 * @return an employee object, type is set according to the permission
		 */
		public Employee create(int number, String name, String userName);
	}
	
	private class EmployeeFac implements Factory {
		
		/**
		 * Gets employee details and creates a new employee accordingly
		 */
		@Override
		public Employee create(int number, String name, String userName) {
			
			// create a new employee according to given details
			return new Employee(number, name, userName);
		}
	}
	
	private class ManagerFac implements Factory {
		
		/**
		 * Gets manager details and creates a new manager accordingly
		 */
		@Override
		public Manager create(int number, String name, String userName) {
			
			// create a new manager according to given details
			return new Manager(number, name, userName);
		}
	}
	
	private Map<CommonValues.permissions, Factory> factoryMap; // holds employee types' factories
	
	/**
	 * Constructor- creates a new employee factory.
	 * Allocates factory for each employee type and stores it in the
	 * factory map
	 */
	public EmployeeFactory() {
		
		// create a new factory map
		factoryMap = new HashMap<CommonValues.permissions, EmployeeFactory.Factory>();
		
		// set factory for each employee type
		factoryMap.put(CommonValues.permissions.EMPLOYEE, new EmployeeFac());
		factoryMap.put(CommonValues.permissions.MANAGER, new ManagerFac());
	}
	
	/**
	 * Gets permission and employee details, and returns a new employee
	 * with given details
	 * @param permission- new employee's permission
	 * @param number- new employee's number
	 * @param name- new employee's name
	 * @param userName- new employee's user-name
	 * @return a new employee according to given details and permission
	 */
	public Employee createNewEmployee(CommonValues.permissions permission, int number, String name, String userName) {
		
		// return a new employee according to given details
		return factoryMap.get(permission).create(number, name, userName);
	}
}